<template>
	<div>
		<jk-dialog
			v-model="showDialog"
			:title="`${dataId ? '编辑' : '新增'}-单据编号`"
			width="520px"
			:show-content-loading="showLoading"
			:confirm-btn-loading="confirmBtnLoading"
			@on-cancel="onVisibleChange(false)"
			@on-visible-change="onVisibleChange"
			@on-confirm="onConfirmBtn"
		>
			<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" :show-message="false" size="large">
				<el-row>
					<el-col :span="23">
						<el-form-item class="margin-bottom-10" label="模块名称" prop="typeName">
							<el-input v-model="ruleForm.typeName" placeholder="模块名称" />
						</el-form-item>
					</el-col>
					<el-col :span="23">
						<el-form-item class="margin-bottom-10" label="前缀" prop="prefix">
							<el-input v-model="ruleForm.prefix" placeholder="前缀" />
						</el-form-item>
					</el-col>
					<el-col :span="23">
						<el-form-item class="margin-bottom-10" label="日期规则" prop="date">
							<el-select v-model="ruleForm.date" placeholder="日期规则" class="width-100-percent">
								<el-option v-for="item in dateRuleList" :key="item.id" :label="item.name" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="23">
						<el-form-item class="margin-bottom-10" label="流水号长度" prop="numLength">
							<el-input-number v-model="ruleForm.numLength" :min="1" :max="20" placeholder="流水号长度" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>

			<jk-divider-card title="模拟生成" placement="center" icon="el-icon-set-up" class="margin-top-10">
				<div class="code-rule-container">
					<div class="code-rule-container-groups">
						<div class="code-rule-container_column">
							<span class="code-rule-container_column-title">前缀</span>
							<el-input readonly :value="ruleForm.prefix"></el-input>
						</div>
						<div class="code-rule-container_column">
							<span class="code-rule-container_column-title">日期规则</span>
							<el-input readonly :value="transferDate(ruleForm.date)"></el-input>
						</div>
						<div class="code-rule-container_column">
							<span class="code-rule-container_column-title">流水号</span>
							<el-input readonly :value="transferNum(ruleForm.numLength)"></el-input>
						</div>
					</div>
					<div class="code-rule-container_column-demo">如：{{ `${ruleForm.prefix}${ transferDate(ruleForm.date) }${ transferNum(ruleForm.numLength) }` }}</div>
				</div>
			</jk-divider-card>

		</jk-dialog>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { spinGenerateCodeSettingCreate, spinGenerateCodeSettingUpdate, spinGenerateCodeSettingGet } from '@/api/basicData/orderCodeSetting';
    import { statusList } from '@/utils/jk/status-option-list';
    import { formatDate, formatTime, formatMonth, tipsBar } from '../../../utils/jk/jk-tool';

    export default {
        name: 'OrderCodeSetting',
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        mixins: [calHeight],
        data() {
            return {
                dateRuleList: [
                    { id: 'yyyy', name: 'yyyy' },
                    { id: 'yyyyMM', name: 'yyyyMM' },
                    { id: 'yyyyMMdd', name: 'yyyyMMdd' },
                    { id: 'yyyyMMddHHmmss', name: 'yyyyMMddHHmmss' },
                    { id: '-1', name: '不包含日期' }
                ],
                statusList: statusList,
                showDialog: false,
                showLoading: true,
                confirmBtnLoading: false,
                saveDialogState: false,
                ruleForm: {
                    id: null,
                    typeName: null,
                    prefix: '',
                    date: '',
                    numLength: 6,
                    tenantId: null,
                    version: null
                },
                rules: {
                    prefix: [{ required: true, trigger: 'change' }],
                    date: [{ required: true, trigger: 'change' }],
                    typeName: [{ required: true, trigger: 'change' }],
                    numLength: [{ required: true, trigger: 'change' }]
                }
            };
        },
        methods: {
            transferNum(len) {
                let str = '';
                for (let i = 0; i < len; i++) {
                    if (i === len - 1) {
                        str += '1';
                    } else {
                        str += '0';
                    }
                }
                return str;
            },
            transferDate(e) {
                let str = '';
                if (e === 'yyyy') {
                    str = new Date().getFullYear();
                } else if (e === 'yyyyMM') {
                    str = formatMonth(new Date(), '');
                } else if (e === 'yyyyMMdd') {
                    str = formatDate(new Date(), '');
                } else if (e === 'yyyyMMddHHmmss') {
                    str = formatTime(new Date(), '');
                }
                return str;
            },
            /** 提交按钮 */
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.confirmBtnLoading = true;
                        const params = JSON.parse(JSON.stringify(this.ruleForm));
                        if (!this.dataId) delete params.id;
                        if (params.date === '-1') {
                            params.date = '';
                        }
                        const api = !this.dataId ? spinGenerateCodeSettingCreate : spinGenerateCodeSettingUpdate;
                        api(params).then(res => {
                            this.confirmBtnLoading = false;
                            if (res.data.status === 200) {
                                this.$tipsBar(this, 'save');
                                this.saveDialogState = false;
                                this.$emit('on-finish');
                            }
                        });
                    } else {
                        tipsBar(this, 'validator');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return spinGenerateCodeSettingGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        const resData = res.data.res;
                        if (!resData.date) {
                            resData.date = '-1';
                        }
                        this.ruleForm = resData;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
<style scoped lang="scss">
.code-rule-container {
    //border: solid 1px #dcdee2;
    padding: 12px;
    .code-rule-container-groups {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 8px;
        .code-rule-container_column {
            width: calc(100% / 3);
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin: 0 1px;
            .code-rule-container_column-title {

            }
        }
    }
    .code-rule-container_column-demo {
        width: 100%;
        font-size: 14px;
        font-weight: bold;
        color: #2d8cf0;
        text-align: center;
    }
}
</style>
